# !/usr/bin/env python
# -*- coding: UTF-8 -*-
# author: xzqttt@gmail.com
# date: 07/22/2011

from pymongo import Connection
import MySQLdb
import datetime

db = 'mms'
host = 'localhost'
port_mongo = 27017
port_mysql = 3306
user = 'root'
passwd = '123456'
page_size = 1000

# 32 bit os limition
max_page = 10

# funcation
def saveToMongo(collection, columns, cursor, sql):
    cursor.execute(sql)
    result_set = cursor.fetchall()
    for result in result_set:
        entity = dict(zip(columns, result))
        collection.save(entity)


mongo_conn = Connection(host, port=port_mongo)
server_info = mongo_conn.server_info()
print '\n===========Server Info:'
for key in server_info:
    print key, ':', server_info[key]

database = mongo_conn[db]

conn = MySQLdb.connect(host, user, passwd, db, port_mysql, charset='utf8')
cursor = conn.cursor()

#fetch database
cursor.execute("SHOW TABLES")
tables = cursor.fetchall();

#echo tables
for table in tables:
    tableName = table[0]
    if 'NOTIFICATION' in tableName:
        continue
    collection = database[tableName]
    print '\n=====================' + tableName + '================='
    startTime = datetime.datetime.now()
    print 'startTime is %s' % startTime
    cmd = "DESC " + (tableName)
    cursor.execute(cmd)
    table_desc = cursor.fetchall()
    columns = []
    for desc in table_desc:
        columns.append(desc[0])
    print columns

    sql = "SELECT COUNT(1) FROM %s" % tableName
    cursor.execute(sql)
    total = cursor.fetchone()[0]
    print 'total is %d' % total

    if total > page_size:
        if total % page_size == 0:
            total_page = total / page_size
        else:
            total_page = (total / page_size) + 1
        print 'total_page is %d' % total_page
        page_number = 1
        while page_number <= total_page:
            if page_number > max_page:
                break
            print 'current page is %d' % page_number
            sql = "SELECT * FROM %s LIMIT %d,%d" % (tableName, (page_number - 1) * page_size, page_size)
            saveToMongo(collection, columns, cursor, sql)
            page_number += 1
    else:
        print 'total_page is %d' % 1
        sql = "SELECT * FROM %s LIMIT 1" % tableName
        saveToMongo(collection, columns, cursor, sql)
    endTime = datetime.datetime.now()
    print 'endTime is %s' % endTime
    print 'Time Token : ', endTime - startTime

cursor.close()
conn.close()

print '\nfinished'
